<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\ProjectRequest;
use App\Models\Job;
use App\Models\Project;
use App\Transformers\ProjectTransformer;
use Illuminate\Http\Request;

class ProjectController extends Controller
{
    public function index(Request $request, Project $project)
    {
        $project = $project->newQuery();
        $project->where('is_deleted', 0);
        if ($request->input('name')) {
            $project->where('name', $request->input('name'));
        }
        if ($request->input('status') || $request->input('status') === 0 || $request->input('status') === '0') {
            $project->where('status', $request->input('status'));
        }
        if ($request->input('principal')) {
            $project->where('principal', $request->input('principal'));
        }
        if ($request->input('department_id')) {
            $project->where('department_id', $request->input('department_id'));
        }
//        if ($request->input('date')) {
//            $date = explode(',', $request->input('date'));
//            $start_time = strtotime($date[0]);
//            $end_time = strtotime($date[1]);
//        }

        $projects = $project->orderBy('created_at', 'desc')->paginate(50);

        return $this->response->paginator($projects, new ProjectTransformer());
    }

    public function store(ProjectRequest $request)
    {
        $time = $request->time;
        $start_time = strtotime($time[0]);
        $end_time = strtotime($time[1]);
        $project = Project::create([
            'name' => $request->name,
            'department_id' => $request->department_id,
            'post' => '',
            'status' => 1,
            'principal' => $request->principal,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'people' => 1
        ]);

        return $this->response->item($project, new ProjectTransformer())
            ->setStatusCode(201);
    }

    public function update(ProjectRequest $request, Project $project)
    {
        $project->update([
            'name' => $request->name,
            'department_id' => $request->department_id,
            'post' => $request->post,
            'principal' => $request->principal,
            'start_time' => $request->start_time,
            'end_time' => $request->end_time,
        ]);

        return $this->response->item($project, new ProjectTransformer());
    }

    public function delete(Project $project)
    {
        $check = Job::where('project_id', $project->id)->first();
        if ($check) {
            return response()->json([
                'status' => 403,
                'message' => '该项目下面有正在进行的职位招聘，不能删除!'
            ]);
        }
        $project->update([
            'is_deleted' => 1
        ]);

        return $this->response->noContent();
    }

    public function all()
    {
        $projects = Project::all();

        return $this->response->collection($projects, new ProjectTransformer());
    }
}
